package com.indusfo.spc.controller;

import com.indusfo.spc.mapper.OrderMapper;
import com.indusfo.spc.pojo.MatVer;
import com.indusfo.spc.pojo.Order;
import com.indusfo.spc.service.OrderFlowService;
import com.indusfo.spc.service.OrderService;
import com.indusfo.spc.vo.JSONObject;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTIgnoredError;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Description 生产任务单接口
 * @Author louk
 * @Date 2019/11/23 11:00
 */
@Controller
@RequestMapping("/service/order")
public class OrderController {


    private static String CREATETIME;

    @Autowired
    private OrderService orderService;

    @Resource
    private OrderFlowService orderFlowService;
    @Resource
    private OrderMapper orderMapper;

    /**
     * 新增
     *
     * @param order
     * @return
     */
    @RequestMapping("insertOrder")
    @ResponseBody
    public JSONObject insertOrder(Order order) {
        order.setDataState(1);
        JSONObject json = orderService.insertPlan(order);
        orderFlowService.leadOrderFlow(order.getMatVerId(),order.getOrderId());
        return json;
    }

    /**
     * 启用停用删除
     */
    @RequestMapping("/definesOrder")
    @ResponseBody
    public JSONObject definesOrder(Integer[] ids, Integer dataState, Integer[] executeStateIds) {

        JSONObject json = orderService.deletePlan(ids, dataState, executeStateIds);
        return json;
    }

    /**
     * 修改
     */
    @RequestMapping("/updateOrder")
    @ResponseBody
    public JSONObject updateOrder(Order order) {
        JSONObject json = orderService.updatePlan(order);
        return json;
    }


    /**
     * 查询
     *
     * @param order
     * @return
     */
    @RequestMapping("/queryOrder")
    @ResponseBody
    public JSONObject queryOrder(Order order) {
        JSONObject json = orderService.queryPlan(order);
        return json;
    }

    /**
     * 状态修改
     *
     * @param ids
     * @param executeStateIds
     * @param executeState
     * @return
     */
    @RequestMapping("/executeStateChange")
    @ResponseBody
    public JSONObject executeStateChange(Integer[] ids, Integer[] executeStateIds, Integer executeState) {
        return orderService.lExecuteStateChange(ids, executeStateIds, executeState);
        /*if(executeState!=null && executeState.equals(2) && json.isOk()){
            for (int i = 0; i < ids.length; i++) {
                if(executeStateIds[i].equals(1)) {
                    Order order = orderMapper.queryOrder(ids[i]);
                    orderFlowService.leadOrderFlow(order.getMatVerId(), ids[i]);
                }
            }

        }*/

    }

    /**
     * 获取日期
     *
     * @return
     */
    @RequestMapping("/getDate")
    @ResponseBody
    public JSONObject getDate() {
        Date date = new Date();
        String strDateFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        CREATETIME = sdf.format(date);
        return JSONObject.build(200, "成功", CREATETIME);
    }

    /**
     * 获取版本
     *
     * @param matId
     * @return
     */
    @RequestMapping("/getVersion")
    @ResponseBody
    public JSONObject getVersion(Integer matId) {
        JSONObject version = orderService.getVersion(matId);
        return version;
    }

    /**
     * 保存工序流程
     */
    @RequestMapping(value = "/saveOrderFlow", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject saveOrderFlow(@RequestBody Order order) {
        JSONObject json = orderService.saveOrderFlow(order);
        return json;
    }

    /**
     * 复制工序流程
     */
    @RequestMapping(value = "/copyOrderFlow", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject copyOrderFlow(@RequestBody Order order) {
        JSONObject json = orderService.copyOrderFlow(order);
        return json;
    }


    /**
     * 保存维修流程
     */
    @RequestMapping(value = "/saveOrderRepair", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject saveOrderRepair(@RequestBody Order order) {
        JSONObject json = orderService.saveOrderRepair(order);
        return json;
    }
    /**
     * 复制维修流程
     */
    @RequestMapping(value = "/copyOrderRepair", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject copyOrderRepair(@RequestBody Order order) {
        JSONObject json = orderService.copyOrderRepair(order);
        return json;
    }


    /**
     * 保存维修流程
     */
    @RequestMapping(value = "/queryOrderList", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject queryOrderList(@RequestBody Order order) {
        JSONObject json = orderService.queryOrderList(order);
        return json;
    }

    /**
     * 复制版本
     */
    @RequestMapping(value = "/copyOrderTech", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject copyOrderTech( Integer orderTechId) {
        JSONObject json = orderService.copyOrderTech(orderTechId);
        return json;
    }

    /**
     * 查询任务单(任务单流程)
     *
     * @param order
     * @return
     */
    @RequestMapping("/selectOrder")
    @ResponseBody
    public JSONObject selectOrder(Order order) {
        JSONObject json = orderService.selectOrder(order);
        return json;
    }
}
